package com.bawei.doctor.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.bawei.doctor.entity.GradeMarkEntity;
import com.bawei.doctor.service.GradeMarkService;
import com.bawei.common.utils.PageUtils;
import com.bawei.common.utils.R;



/**
 * 医院,科室,医生等级三级表
 *
 * @author wtj
 * @email 1756179378@qq.com
 * @date 2021-12-09 16:49:01
 */
@RestController
@RequestMapping("doctor/grademark")
public class GradeMarkController {
    @Autowired
    private GradeMarkService gradeMarkService;

    @Autowired
    private StringRedisTemplate redisTemplate;
    //doctor/grademark/hospitalList
    //roomList
    @GetMapping("/hospitalList")
    public R hospitalList(){
//为了减轻数据库的压力，把医院列表缓存在Redis中，除了第一次下拉框从表中加载，其他情况则从缓存中获取
        //判断 redis中有无数据
        ArrayList<GradeMarkEntity> list = new ArrayList<>();
        String hospitals = redisTemplate.opsForValue().get("hospitals");
        if (hospitals!=null &&!"".equals(hospitals)){
            List<GradeMarkEntity> hospitalList = JSON.parseArray(hospitals,GradeMarkEntity.class);
            return R.ok().put("data", hospitalList);
        }
        //没有数据，存redis
        List<GradeMarkEntity> hospitalList = gradeMarkService.hospitalList();
        String s = JSON.toJSONString(hospitalList);
        redisTemplate.opsForValue().set("hospitals",s);
        return R.ok().put("data", hospitalList);
    }

    @GetMapping("/listGraedMark")
    public List<GradeMarkEntity> listGraedMark(){
        return gradeMarkService.list();
    }

    @GetMapping("/roomList/{pid}")
    public R roomList(@PathVariable("pid")Integer pid ){
        List<GradeMarkEntity> hospitalList = gradeMarkService.roomList(pid);
        return R.ok().put("data", hospitalList);
    }


    @GetMapping("/workList/{pid}")
    public R workList(@PathVariable("pid")Integer pid ){
        List<GradeMarkEntity> hospitalList = gradeMarkService.roomList(pid);
        return R.ok().put("data", hospitalList);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = gradeMarkService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		GradeMarkEntity gradeMark = gradeMarkService.getById(id);

        return R.ok().put("gradeMark", gradeMark);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody GradeMarkEntity gradeMark){
		gradeMarkService.save(gradeMark);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GradeMarkEntity gradeMark){
		gradeMarkService.updateById(gradeMark);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		gradeMarkService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
